'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('purchase_order_items', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '采购订单ID'
      },
      product_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '产品ID'
      },
      product_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '产品名称'
      },
      specification: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '规格'
      },
      quantity: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '数量'
      },
      unit: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: '单位'
      },
      unit_price: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
        comment: '单价'
      },
      amount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
        comment: '金额'
      },
      remark: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '备注'
      },
      created_at: {
        type: Sequelize.DATE
      },
      updated_at: {
        type: Sequelize.DATE
      }
    });

    // 添加索引
    await queryInterface.addIndex('purchase_order_items', ['order_id'], {
      name: 'idx_purchase_order_items_order_id'
    });

    await queryInterface.addIndex('purchase_order_items', ['product_id'], {
      name: 'idx_purchase_order_items_product_id'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('purchase_order_items');
  }
};'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('purchase_order_items', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      order_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '采购订单ID'
      },
      product_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '产品ID'
      },
      product_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '产品名称'
      },
      specification: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '规格'
      },
      quantity: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '数量'
      },
      unit: {
        type: Sequelize.STRING(20),
        allowNull: true,
        comment: '单位'
      },
      unit_price: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
        comment: '单价'
      },
      amount: {
        type: Sequelize.DECIMAL(15, 2),
        allowNull: false,
        comment: '金额'
      },
      remark: {
        type: Sequelize.STRING(200),
        allowNull: true,
        comment: '备注'
      },
      created_at: {
        type: Sequelize.DATE
      },
      updated_at: {
        type: Sequelize.DATE
      }
    });

    // 添加索引
    await queryInterface.addIndex('purchase_order_items', ['order_id'], {
      name: 'idx_purchase_order_items_order_id'
    });

    await queryInterface.addIndex('purchase_order_items', ['product_id'], {
      name: 'idx_purchase_order_items_product_id'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('purchase_order_items');
  }
};